package com.wejoy.video.middle.station.data.service.workorder.impl;

import com.wejoy.video.middle.station.data.domain.system.SysNotice;
import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderInfo;
import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderReview;
import com.wejoy.video.middle.station.data.enums.dict.*;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.mapper.workorder.IWorkOrderReviewMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysNoticeService;
import com.wejoy.video.middle.station.data.service.workorder.IWorkOrderInfoService;
import com.wejoy.video.middle.station.data.service.workorder.IWorkOrderReviewService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 工单审核记录
 *
 * @author miracle_j
 * @date 2022年11月17日 17:39:37
 */
@Service
public class WorkOrderReviewServiceImpl implements IWorkOrderReviewService {
    @Autowired
    private IWorkOrderReviewMapper workOrderReviewMapper;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Autowired
    private IWorkOrderInfoService   workOrderInfoService;
    @Autowired
    private ISysNoticeService sysNoticeService;

    @Override
    public void save(WorkOrderReview req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            processApproval(req);
        } else {
            updateSelectiveById(req);
        }
    }

    /**
     * 处理审核逻辑
     * @param req
     */
    private void processApproval(WorkOrderReview req) {
        WorkOrderInfo workOrderInfo = workOrderInfoService.getOneById(req.getOrderId());
        if(ObjectUtils.isEmpty(workOrderInfo)){
            throw new BusinessException("参数错误");
        }
        if(!workOrderInfo.getOrderStatus().equals(EnumWorkOrderStatus.ACCEPT_PROCESSED.getCode())){
            throw new BusinessException("此工单已被审核过！");
        }
        if(req.getReviewStatus().equals(EnumWorkOrderReviewStatus.EXAMINATION_PASSED.getCode())){
            workOrderInfo.setOrderStatus(EnumWorkOrderStatus.FINISHED.getCode());
        }else {
            workOrderInfo.setOrderStatus(EnumWorkOrderStatus.ACCEPT_UNPROCESSED.getCode());
            auditFailureNotification(workOrderInfo,req);
        }
        insertSelective(req);
        workOrderInfoService.save(workOrderInfo, req.getOperator());
    }

    /**
     * 审核失败通知发送
     * @param workOrderInfo
     * @param workOrderReview
     */
    private void auditFailureNotification(WorkOrderInfo workOrderInfo,WorkOrderReview workOrderReview){
        SysNotice sysNoticeDB = new SysNotice();
        sysNoticeDB.setOrderId(workOrderInfo.getId());
        sysNoticeDB.setIsRead(EnumReadNoticeType.HAVE_READ.getCode());
        sysNoticeDB.setRemindType(EnumNoticePopUpsType.WARNING_POPUPS.getCode());
        sysNoticeDB.setTitle(EnumNoticeType.AUDIT_REJECT_NOTICE.getText());
        sysNoticeDB.setType(EnumNoticeType.AUDIT_REJECT_NOTICE.getCode());
        sysNoticeDB.setToId(workOrderInfo.getAcceptUserId());
        sysNoticeDB.setContent(workOrderInfo.getDeptName()+","+workOrderInfo.getAlarmLocation());
        sysNoticeService.save(sysNoticeDB,workOrderReview.getOperator());
    }

    @Override
    public int insertSelective(WorkOrderReview req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return workOrderReviewMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(WorkOrderReview workOrderReview) {
        WJAssert.notNull(workOrderReview, "入参对象不能为空");
        WJAssert.notNull(workOrderReview.getId(), "id参数错误");
        WorkOrderReview where = new WorkOrderReview();
        where.setId(workOrderReview.getId());
        return workOrderReviewMapper.updateSelective(workOrderReview, where);
    }

    @Override
    public int getCount(WorkOrderReview workOrderReview) {
        checkStringLength(workOrderReview, true);
        return workOrderReviewMapper.getCount(workOrderReview);
    }

    @Override
    public WorkOrderReview getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        WorkOrderReview where = new WorkOrderReview();
        where.setId(id);
        return workOrderReviewMapper.getOne(where);
    }

    @Override
    public List<WorkOrderReview> getList(WorkOrderReview workOrderReview) {
        checkStringLength(workOrderReview, true);
        return workOrderReviewMapper.getList(workOrderReview);
    }

    @Override
    public List<WorkOrderReview> getPageList(WorkOrderReview workOrderReview) {
        checkStringLength(workOrderReview, true);
        return workOrderReviewMapper.getPageList(workOrderReview);
    }

    @Override
    public void audit(Long id, String operator) {
        WorkOrderReview req = new WorkOrderReview();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        WorkOrderReview req = new WorkOrderReview();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        WorkOrderReview req = new WorkOrderReview();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(WorkOrderReview workOrderReview, boolean nullAble) {
        WJAssert.notNull(workOrderReview, "入参对象不能为空");
        WJAssert.limitMaxLength(workOrderReview.getOperator(), nullAble, 100, "操作人不合法!");
        WJAssert.limitMaxLength(workOrderReview.getRemark(), true, 255, "备注不合法!");
    }
}
